﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sita.MainModule.Services;
using Sita.Entities;
using Sita.Presentation.ViewModels;

namespace Sita.Presentation.Controllers
{
    public class CustomerController : DefaultController
    {
        CustomerService customerService;

        public CustomerController()
        {
            customerService = new CustomerService();
        }

        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult List(int? index, string sortColumn, bool? sortAscending, int? pageSize, string firstName, string lastName, int? dni, string phone, string email)
        {
            int TotalRows;

            int CurrentBusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;
            sortAscending = sortAscending ?? true;
            index = index ?? 0;
            dni = dni ?? 0;
            if (string.IsNullOrEmpty(sortColumn))
                sortColumn = "FirstName";
            if (!pageSize.HasValue)
                pageSize = 5;

            ViewData["PageSizeFilter"] = new SelectList(new int[] { 5, 10, 15, 20 });
            ViewData["FirstName"] = firstName;
            ViewData["LastName"] = lastName;
            ViewData["DNI"] = dni;
            ViewData["Phone"] = phone;
            ViewData["Email"] = email;


            return PartialView(
                    new GenericGridList<Customer>(
                        customerService.GetPaginatedByFilter(CurrentBusinessUnitID, index.Value, pageSize.Value, sortColumn, sortAscending.Value, firstName, lastName, dni.Value, phone, email, out TotalRows), index.Value, sortColumn, pageSize.Value, TotalRows, sortAscending.Value));
        }

        [HttpGet]
        public PartialViewResult Filter()
        {
            return PartialView(new FilterModel() { PageSize = 5 });
        }

        #region CREATE
        [HttpGet]
        public PartialViewResult Create()
        {
            return PartialView("Create");
        }

        [HttpPost]
        public JsonResult Create(Customer customer)
        {
            JsonResult Response;

            customer.BusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;

            customerService.AddCustomer(customer);

            Response = new JsonResult();
            Response.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            Response.Data = customer.CustomerID > 0;

            return Json(Response);
        }
        #endregion

        #region EDIT
        [HttpGet]
        public PartialViewResult Edit(int? id)
        {
            if (id.HasValue)
            {
                Customer selectedCustomer = customerService.GetCustomerByID(id.Value);
                return PartialView(selectedCustomer);
            }
            else
            {
                //return PartialView("PageNotFound", "Errors");
                return null;
            }
        }

        [HttpPost]
        public JsonResult Edit(Customer customer)
        {
            Customer OriginalCustomer;
            JsonResult Response;

            OriginalCustomer = customerService.GetCustomerByID(customer.CustomerID);

            Response = new JsonResult();
            Response.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (OriginalCustomer != null)
            {
                TryUpdateModel<Customer>(OriginalCustomer);

                customerService.ModifyCustomer(OriginalCustomer);

                Response.Data = true;
            }
            else
            {
                Response.Data = false;
            }

            return Json(Response);
        }

        #endregion

        public JsonResult Delete(int? id)
        {
            JsonResult JsonResponse;
            Customer DeleteCustomer;
            Dictionary<string, object> ResultDetails;

            ResultDetails = new Dictionary<string, object>();

            ResultDetails.Add("Message", string.Empty);
            ResultDetails.Add("Success", string.Empty);

            JsonResponse = new JsonResult();

            JsonResponse.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (id.HasValue)
            {
                DeleteCustomer = customerService.GetCustomerByIDWithVehicles(id.Value);

                if (DeleteCustomer != null)
                {
                    if (DeleteCustomer.Vehicles.Count > 0)
                    {
                        ResultDetails["Success"] = false;
                        ResultDetails["Message"] = Resources.Employee.Index.ConfirmDeleteWithChildsMessage;
                    }
                    else
                    {
                        if (customerService.DeleteCustomer(DeleteCustomer))
                        {
                            ResultDetails["Success"] = true;
                        }
                        else
                        {
                            ResultDetails["Success"] = false;
                            ResultDetails["Message"] = Resources.Customer.Index.CannotDelete;
                        }
                    }
                }
                else
                {
                    ResultDetails["Success"] = false;
                    ResultDetails["Message"] = Resources.Customer.Index.InvalidCustomer;
                }
            }
            else
            {
                ResultDetails["Success"] = false;
            }

            JsonResponse.Data = ResultDetails;

            return JsonResponse;
        }

        public JsonResult GetCustomersByBusinessUnitID(string companyName,string sortColumn)
        {
            int CurrentBusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;
            IList<Customer> Customers;

            Customers = customerService.GetCustomersByBusinessUnitIDByCompanyName(CurrentBusinessUnitID, companyName, sortColumn);

            return Json(Customers, JsonRequestBehavior.AllowGet);
        }
    }
}
